package com.jz.beijianbao.price.service.impl;

import com.jz.beijianbao.beijian.bean.BeiJian;
import com.jz.beijianbao.beijian.bean.BeiJianSelectVo;
import com.jz.beijianbao.beijian.mapper.BeiJianMapper;
import com.jz.enu.CurrencyCodeEnum;
import com.jz.enu.PriceEnum;
import com.jz.enu.StatusEnum;
import com.jz.beijianbao.legalperson.bean.LegalPerson;
import com.jz.beijianbao.price.bean.*;
import com.jz.beijianbao.price.mapper.PriceMapper;
import com.jz.beijianbao.price.service.PriceService;
import com.jz.beijianbao.subclass.bean.StatusVo;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class PriceServiceImpl implements PriceService {

    @Autowired
    private PriceMapper priceMapper;
    private BeiJianMapper beiJianMapper;

//法人下拉
    @Override
    public ResultVO personXiaLa() {
        List<LegalPerson> legalPeople =  priceMapper.queryPerson();
        return ResultBuildVO.success(legalPeople);
    }
//含税下拉
    @Override
    public ResultVO queryHanShui() {
        List<TaxVo> taxVoList =  priceMapper.queryHanShui();
        return ResultBuildVO.success(taxVoList);
    }
//三个下拉
    @Override
    public ResultVO queryPullDownList1() {
        List<StatusVo> statusVoList = new ArrayList<>();
        List<CurrencyVo> currencyVoList = new ArrayList<>();
        List<PriceTypeVo> priceTypeVoList = new ArrayList<>();
        PutDownVo putDownVo = new PutDownVo();
        for (StatusEnum value : StatusEnum.values()) {
            StatusVo statusVo = new StatusVo(value.getStatus(),value.getStatusName());
            statusVoList.add(statusVo);
        }
        putDownVo.setStatusList(statusVoList);
        for (PriceEnum value : PriceEnum.values()) {
            PriceTypeVo priceTypeVo = new PriceTypeVo(value.getPriceType(),value.getPriceTypeName());
            priceTypeVoList.add(priceTypeVo);
        }
        putDownVo.setJizhang(priceTypeVoList);
        for (CurrencyCodeEnum value : CurrencyCodeEnum.values()) {
            CurrencyVo currencyVo = new CurrencyVo(value.getCurrencyCode(),value.getCurrencyCodeName());
            currencyVoList.add(currencyVo);
        }
        putDownVo.setMoney(currencyVoList);

        return ResultBuildVO.success(putDownVo);

    }
//分页
    @Override
    public ResultVO queryPage(PriceSelectVo priceSelectVo) {

        List<Price> priceList = priceMapper.queryPage(priceSelectVo);
        List<String> codeList = new ArrayList<>();
        for (Price priceVo : priceList) {
            Integer status = priceVo.getStatus() ? 1:0;
            priceVo.setStatusName(StatusEnum.getName(status));

            Integer priceType = priceVo.getPriceType();
            priceVo.setPriceTypeName(PriceEnum.getName(priceType));

            Integer currency = priceVo.getCurrencyCode();
            priceVo.setCurrencyCodeName(CurrencyCodeEnum.getName(currency));
            codeList.add(priceVo.getCode());
        }
        if(!CollectionUtils.isEmpty(codeList)){
            List<SparePersonVo> sparePersonVoList = priceMapper.querySparePerson(codeList);
            for (Price priceVo : priceList) {
                List<String> legalPersonNameList = new ArrayList<>();
                for (SparePersonVo sparePersonVo : sparePersonVoList) {
                    if (priceVo.getCode().equals(sparePersonVo.getCode())){
                        legalPersonNameList.add(sparePersonVo.getLegalPersonName());
                    }
                }
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < legalPersonNameList.size(); i++) {
                    sb.append(legalPersonNameList.get(i));
                    if (i != legalPersonNameList.size()-1){
                        sb.append(",");
                    }
                }
                priceVo.setLegalPersonName(sb.toString());
            }
        }
        int count = priceMapper.queryCount(priceSelectVo);

        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setPageIndex(priceSelectVo.getPageIndex());
        allRecordsVo.setPageSize(priceSelectVo.getPageSize());
        allRecordsVo.setTotalSize(count);
        allRecordsVo.countPage(count);
        allRecordsVo.setDataList(priceList);
        return ResultBuildVO.success(allRecordsVo);
    }
//备件查询
    @Override
    public ResultVO querySearchEnabled(BeiJianSelectVo spareSelectVo) {
        List<BeiJian> beiJianSelectVo1 = priceMapper.querySearchEnabled(spareSelectVo);
        int count = priceMapper.count(spareSelectVo);
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setPageIndex(spareSelectVo.getPageIndex());
        allRecordsVo.setPageSize(spareSelectVo.getPageSize());
        allRecordsVo.setTotalSize(count);
        allRecordsVo.countPage(count);
        allRecordsVo.setDataList(beiJianSelectVo1);
        return ResultBuildVO.success(allRecordsVo);
    }

    @Override
    public ResultVO add(PriceAdd priceAdd) {
//        List<Price> priceList = priceMapper.queryQuanBu();
//        if (priceAdd.get)









        return null;
    }












}
